<?php

namespace apiend\modules\work\models;

use Yii;
use common\helpers\ArrayHelper;
use common\helpers\StringHelper;
use common\logics\cascader\CompanyLogic;

/**
 * 人员检索模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class StaffSearch extends \common\models\work\Staff {

    /**
     * @var string 关键字
     */
    public $keyword;

    /**
     * @var array 状态
     */
    public $companyIds = [];

    /**
     * @var status 状态
     */
    public $sort = [
        'defaultOrder' => [
            'created_at' => SORT_DESC,
        ]
    ];

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['keyword', 'companyIds', 'status'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function getActiveStatus() {
        $status = $this->getMarksOptionList('status');
        return ArrayHelper::getValue($status, $this->status);
    }

    /**
     * Searching member apply
     * @return \common\db\ActiveQuery
     */
    public function buildQuery() {
        $alias = 't';
        $query = static::find();
        if ($alias) {
            $query->alias($alias);
            $alias .= '.';
        }
        $this->bindKeywords($query, $alias);
        $companyIds = ArrayHelper::merge($this->companyIds, Yii::$app->user->identity->companyIds);
        $query->andFilterWhere([
            $alias . 'company_id' => $companyIds,
        ]);
        $query->andFilterWhere([
            $alias . 'status' => $this->status,
        ]);
        return $query;
    }

    /**
     * 关键词检索
     * @param \common\db\ActiveQuery $query
     */
    protected function bindKeywords($query, $alias = 't') {
        $word = trim($this->keyword);
        if (!$word) {
            return;
        }
        $condition = [
            'or',
        ];
        if (StringHelper::isNumber($word)) {
            $condition[] = [$alias . 'user_id' => $word];
            $condition[] = [$alias . 'id' => $word];
            if (StringHelper::isMobile($word)) {
                $entra = Yii::$app->security->generatePrivateKey($word);
                $condition[] = [$alias . 'mobile' => $entra];
            }
        } else {
            $condition[] = ['like', $alias . 'realname', $word];
        }
        $query->andWhere($condition);
    }

    /**
     * {@inheritdoc}
     */
    public function fieldConfigs() {
        return [
            [
                'attribute' => 'id',
                'headerOptions' => ['width' => 80],
            ],
            'realname',
            [
                'attribute' => 'fullMobile',
            ],
            [
                'label' => '绑定用户',
                'attribute' => 'user',
                'format' => 'user',
                'options' => [
                    'nullText' => '未入驻',
                ],
                'headerOptions' => ['width' => '150px'],
            ],
            [
                'attribute' => 'companyName',
                'label' => '所属街道',
            ],
            [
                'class' => 'common\rest\grid\LabelColumn',
                'attribute' => 'status',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'updated_at',
                'format' => 'datetimeRow',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'class' => 'common\rest\grid\ActionColumn',
                'template' => '{update}',
                'headerOptions' => ['width' => '120'],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getCompanyList() {
        return CompanyLogic::findDeeps(1)->all();
    }

    /**
     * {@inheritdoc}
     */
    public function getStatusList() {
        return array_values($this->attributeConstOptions('status'));
    }

    /**
     * {@inheritdoc}
     */
    public function configs() {
        return [
            'dialog' => [
                'visible' => false,
                'title' => '内容设置'
            ],
            'config' => [
                'filterConfig' => [
                    [
                        'label' => '关键字',
                        'prop' => 'keyword',
                        'type' => 'text',
                        'placeholder' => '姓名/手机号/用户ID/人员ID',
                    ],
                    [
                        'label' => '公司',
                        'prop' => 'companyIds',
                        'multiple' => true,
                        'type' => 'cascader',
                        'placeholder' => '按公司检索',
                        'props' => [
                            'multiple' => true,
                            'emitPath' => false,
                            'value' => 'id',
                            'label' => 'name',
                        ],
                        'collapseTags' => true,
                        'data' => $this->companyList,
                    ],
                    [
                        'label' => '人员状态',
                        'placeholder' => '按人员状态筛选',
                        'prop' => 'status',
                        'type' => 'select',
                        'multiple' => false,
                        'collapseTags' => true,
                        'valueKey' => 'value',
                        'labelKey' => 'label',
                        'data' => $this->getStatusList(),
                    ],
                ],
                'gridConfig' => $this->options(),
            ]
        ];
    }

}
